package com.rxst.msg.server.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rxst.msg.dto.search.MsgxIpSearchDto;
import com.rxst.msg.entity.MsgxIpEntity;
import com.rxst.msg.mapper.MsgxIpMapper;
import com.rxst.msg.server.MsgxIpService;
import com.rxst.msg.utils.IpUtil;
import com.rxst.msg.vo.MsgxIpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 白名单
 *
 * @author rxst
 * @email rxst@gmail.com
 * @date 2021-08-19 11:47:00
 */
@Service("msgxIpService")
@Transactional(rollbackFor = Exception.class)
public class MsgxIpServiceImpl extends ServiceImpl<MsgxIpMapper, MsgxIpEntity> implements MsgxIpService {

    @Autowired
    private MsgxIpMapper mapper;

    private List<String> excludePath;

    private List<String> excludeTokenPath;

    @Override
    public List<MsgxIpVo> list(MsgxIpSearchDto vo) {
        return mapper.list(vo);
    }

    @Override
    public PageInfo<MsgxIpVo> page(MsgxIpSearchDto vo) {
        PageHelper.startPage(vo.getCurrent(), vo.getSize());
        PageInfo<MsgxIpVo> page = new PageInfo<>(mapper.list(vo));
        return page;
    }

    @Override
    public Boolean saveEntity(MsgxIpEntity t) {
        t.setIpId(IdUtil.fastSimpleUUID());
        save(t);
        IpUtil.addIp(t);
        return true;
    }

    @Override
    public Boolean saveEntityBatch(List<MsgxIpEntity> list) {
        if (CollUtil.isNotEmpty(list)) {
            for (MsgxIpEntity entity : list) {
                //todo
            }
            return saveBatch(list);
        }
        return false;
    }

    @Override
    public Boolean updateEntity(MsgxIpEntity entity) {
        updateById(entity);
        IpUtil.updateIp(entity);
        return true;
    }

    @Override
    public Boolean updateEntityBatch(List<MsgxIpEntity> list) {
        updateBatchById(list);
        IpUtil.updateIpBath(list);
        return true;
    }

    @Override
    public void deleteLogic(Long id) {
        mapper.deleteLogic(id);
        IpUtil.removeIp(id);
    }

    @Override
    public void deleteLogicByIds(List<Long> ids) {
        mapper.deleteLogicByIds(ids);
        IpUtil.removeIp(ids);
    }

    @Override
    public void initCache() {
        IpUtil.ipClear();
        List<MsgxIpEntity> list = list();
        IpUtil.addIpBath(list);
    }

    @Override
    public List<String> excludePath() {
        if (CollUtil.isEmpty(excludePath)) {
            excludePath = new ArrayList<>();
            excludePath.add("/rxst/msg/user/login");
            excludePath.add("/rxst/msg/user/logOut");
//            excludePath.add("/ip/cache");
//            excludePath.add("/file/upload");
//            excludePath.add("/file/merge");
//            excludePath.add("/file/upload/check");
        }
        return excludePath;
    }

    @Override
    public List<String> excludeTokenPath() {
        if (CollUtil.isEmpty(excludeTokenPath)) {
            excludeTokenPath = new ArrayList<>();
            excludeTokenPath.add("/rxst/msg/file/upload");
            excludeTokenPath.add("/rxst/msg/file/merge");
            excludeTokenPath.add("/rxst/msg/file/upload/check");
        }
        return excludeTokenPath;
    }
}

